from flask import Flask, request, jsonify
from flask_cors import CORS
from config import API_KEY, MODEL_NAME
from rag.rag_cgpt import AdaptiveRAGPipeline

app = Flask(__name__)
CORS(app)

#rag = AdaptiveRAGPipeline(api_key=API_KEY, model_name=MODEL_NAME)
rag = AdaptiveRAGPipeline()

@app.route("/api/ask", methods=["POST", "GET"])
def ask():
    data = request.get_json()
    question = data.get("question", "").strip()

    if not question:
        return jsonify({"error": "Question cannot be empty"}), 400

    try:
        print('question: ', question)
        answer = rag.query(question)
        print('answer: ', answer)
        return jsonify({"answer": answer})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)
